package infra

import (
	"io"
	"mime/multipart"
	"os"
	"strconv"
	"strings"
	"time"

	"gitee.com/h2ob/base"
)

func GetFileType(file string) string {
	idx := strings.LastIndex(file, ".")
	if idx < 0 {
		return "none"
	}
	return string(file[idx:])
}

func FileCopy(src multipart.File, dest string) error {
	destination, err := os.OpenFile(dest, os.O_CREATE|os.O_RDWR, 0766)
	if err != nil {
		base.GLogger.Infof("OpenFile file %s failed:%s", dest, err)
		return err
	}
	_, err = io.Copy(destination, src)
	destination.Close()
	return err
}

func strToInt64(str string) uint64 {
	v, _ := strconv.ParseInt(str, 10, 64)
	return uint64(v)
}

func timeAddNDay(t time.Time, d int) time.Time {
	day := time.Duration(d * 24 * int(time.Hour))
	return t.Add(day)
}
